package com.term.appadmin.controller.role;

import com.term.appadmin.controller.BaseController;
import com.term.appadmin.pojo.query.role.RoleListQuery;
import com.term.appadmin.pojo.request.role.RoleRequest;
import com.term.dao.IBaseDao;
import com.term.domain.Role;
import com.term.domain.utils.DomainPage;
import com.term.service.IRoleService;
import com.term.util.annotation.type.FuzzyQueryUtils;
import com.term.util.response.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by chenGang on 2017/2/9.
 */
@Controller
public class RoleController extends BaseController {

    @Resource
    private IRoleService roleService;

    @RequestMapping(value = "/term/role/list")
    public String list(Model model, @Valid RoleListQuery query) {
        this.setPageTitle(model, "角色列表");
        Map<String, Object> map = FuzzyQueryUtils.convertQueryToMap(query);
        DomainPage<Role> domainPage = roleService.getEntitiesPagesByFieldList(Role.class, map, "id", IBaseDao.SortBy.DESC, query.getIndex(), query.getSize());

        return "/role/list";

    }

    @RequestMapping(value = "/term/role/add", method = RequestMethod.GET)
    public String add(Model model) {
        this.setPageTitle(model, "角色添加");
        this.enableGoBack(model);
        return "/role/add";
    }

    @ResponseBody
    @RequestMapping(value = "/term/role/add", method = RequestMethod.POST)
    public AjaxResponse postAdd(@Valid @RequestBody RoleRequest roleRequest, BindingResult result) {
        if (result.hasErrors()) {
            return this.defaultError(result);
        }


        return AjaxResponse.ok();
    }
}

